//
//  DNTestPlugin.m
//  VipCard_iOS
//
//  Created by zzc on 2019/7/26.
//  Copyright © 2019 tantu.com. All rights reserved.
//

#import "DNTestPlugin.h"
//#import <ZZCCordova/CDVPlugin+CustomPlugin.h>
//#import <CDV.h>
#import <WebKit/WebKit.h>
//#import "UPAPayPlugin.h"
#import <PassKit/PassKit.h>
//#import <SDWebImage/UIImage+WebP.h>
//#import <SDWebImage/SDWebImageDownloaderOperation.h>
//#import <SDWebImage/SDWebImageDownloader.h>
//#import <SDWebImage/SDWebImageManager.h>
#import <ewgShop-Swift.h>
@interface DNTestPlugin ()<WKScriptMessageHandler>

@end


@implementation DNTestPlugin


- (void)isTest:(CDVInvokedUrlCommand *)command
{
    /*
     @property (nonatomic, readonly) NSArray* arguments;
     @property (nonatomic, readonly) NSString* callbackId;
     @property (nonatomic, readonly) NSString* className;
     @property (nonatomic, readonly) NSString* methodName;
     */
    
    NSArray *array = command.arguments;
    NSString *callbackId = command.callbackId;
    NSString *className = command.className;
    NSString *methodName = command.methodName;
    NSString *firArg = [command argumentAtIndex:0 withDefault:@"Firstdefault"];
    NSString *secArg = [command argumentAtIndex:1 withDefault:@"secDefault"];
    NSString *thirdArg = [command argumentAtIndex:2 withDefault:@"thirdDefault"]; NSLog(@"array:%@===callbackId:%@===className:%@===methodName:%@===firArg:%@====secArg:%@====thirdArg:%@",array,callbackId,className,methodName,firArg,secArg,thirdArg);
    
    NSDictionary *callBackParam = @{@"callBackParam" : @"123",@"isSuccess":@YES};
    
    
    
    
    [self.commandDelegate runInBackground:^{
//        [self responseWithCallbackId:callbackId state:0 message:@"success" data:callBackParam];
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:SWIFT_CDVCommandStatus_OK messageAsString:@"success"] callbackId:callbackId];
    }];
}

- (void)jsCallNativeWithOutCallBack:(CDVInvokedUrlCommand *)command
{
    NSArray *array = command.arguments;
    NSString *callbackId = command.callbackId;
    NSString *className = command.className;
    NSString *methodName = command.methodName;
    NSString *firArg = [command argumentAtIndex:0 withDefault:@"Firstdefault"];
    NSString *secArg = [command argumentAtIndex:1 withDefault:@"secDefault"];
    NSString *thirdArg = [command argumentAtIndex:2 withDefault:@"thirdDefault"];
    NSLog(@"array:%@===callbackId:%@===className:%@===methodName:%@===firArg:%@====secArg:%@====thirdArg:%@",array,callbackId,className,methodName,firArg,secArg,thirdArg);
    
    ViewController *vc = [ViewController new];
    vc.startPage = @"https://wx1.ejiaofei.com/entry?memberId=M0100&callbackUrl=knightRecharge%2FphoneRechargeH5&openId=THsvzF5Dvf9VtgxpK7em3w%3D%3D&sign=54DD1CB17FE51B00E0523145599BF041";
    [self.viewController.navigationController pushViewController:vc animated:YES];
    
    
    // 调用js方法
    [self.webViewEngine evaluateJavaScript:[NSString stringWithFormat:@"%@ (%@)",@"nativeCallJs",@"123234"] completionHandler:^(id param, NSError *error) {
        NSLog(@"call js completion :%@===error:%@",param,error.description);
    }];
    
    NSLog(@"viewcontroller class:%@",self.viewController);
}

- (void)jsCallNativeRegisterScriptHandle:(CDVInvokedUrlCommand *)command
{
    NSLog(@"self :%@",self);
    CDVViewController *webVC = (CDVViewController*)self.viewController;
    
    WKWebView *wkWebView = (WKWebView*)webVC.webView;
    
    [wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"TestPluginHandle"];
}

#pragma make WKScriptMessageHandler

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    NSString *name = message.name;
    id param = message.body;
    NSLog(@"Test userContentController :%@===%@",name,param);
}

- (void)dealloc
{
    CDVViewController *webVC = (CDVViewController*)self.viewController;
    
    WKWebView *wkWebView = (WKWebView*)webVC.webView;
    
    [wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"TestPluginHandle"];
}

- (void)openApplePay:(CDVInvokedUrlCommand *)command
{
    NSLog(@"come here");
//    PKPaymentButton *btn = [[PKPaymentButton alloc]init];
    [self.commandDelegate runInBackground:^{
        if (@available(iOS 9.2, *)) {
            BOOL isOkForApplePay = [PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay]];
            NSLog(@"isOkForApplePay :%@",@(isOkForApplePay));
            if (isOkForApplePay) {
                dispatch_async(dispatch_get_main_queue(), ^{
//                    [UPAPayPlugin startPay:@"00" mode:@"01" viewController:self.viewController delegate:self andAPMechantID:@"sgcshdaskjdk732"];
                });
            }
        } else {
            // Fallback on earlier versions
            
        }
    }];
}

//- (void)UPAPayPluginResult:(UPPayResult *)result
//{
//    if(result.paymentResultStatus == UPPaymentResultStatusSuccess) {
//        NSString *otherInfo = result.otherInfo?result.otherInfo:@"";
//        NSString *successInfo = [NSString stringWithFormat:@"支付成功\n%@",otherInfo]; [self showAlertMessage:successInfo];
//    }
//    else if(result.paymentResultStatus == UPPaymentResultStatusCancel){
//        [self showAlertMessage:@"支付取消"]; }
//    else if (result.paymentResultStatus == UPPaymentResultStatusFailure) {
//        NSString *errorInfo = [NSString stringWithFormat:@"%@",result.errorDescription];
//        [self showAlertMessage:errorInfo]; }
//    else if (result.paymentResultStatus == UPPaymentResultStatusUnknownCancel) {
//        //TODO UPPAymentResultStatusUnknownCancel表示发起支付以后用户取消，导致支付状态不确认，需 要查询商户后台确认真实的支付结果
//        NSString *errorInfo = [NSString stringWithFormat:@"支付过程中用户取消了，请查询后台确认订单"]; [self showAlertMessage:errorInfo];
//    }
//}

- (void)showAlertMessage:(id)param
{
    
}

- (void)loadWebPImageAction:(CDVInvokedUrlCommand *)command
{
//    NSArray *array = command.arguments;
    NSString *callbackId = command.callbackId;
//    NSString *className = command.className;
//    NSString *methodName = command.methodName;
    NSString *firArg = [command argumentAtIndex:0 withDefault:@""];
    [self.commandDelegate runInBackground:^{
//        [SDWebImageManager sharedManager].delegate = self;
//        [[[SDWebImageManager sharedManager]imageDownloader]downloadImageWithURL:[NSURL URLWithString:firArg] options:SDWebImageDownloaderContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
//
//        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
//            NSData *imageData = [NSData dataWithData:data];
//            NSString *imageBase64Str = [imageData base64EncodedStringWithOptions:0];
//            NSLog(@"imageBase64Str :%@",data);
//            NSDictionary *callBackParam = @{@"webPData":[NSString stringWithFormat:@"%@",imageBase64Str]};
//            [self responseWithCallbackId:callbackId state:0 message:@"" data:callBackParam];
//        }];
     [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:SWIFT_CDVCommandStatus_OK messageAsString:@"success"] callbackId:callbackId];
    }];
    
    
}
//
//- (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL
//{
//   NSData *pngData = UIImagePNGRepresentation(image);
//    return [UIImage imageWithData:pngData];
//}



@end
